// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const goodDB = cloud.database().collection("good")
// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.op) {
    case "add":
      return goodDB.add({
        data: {
          i_id: event.i_id,
          price: event.price,
          number: event.number,
          name: event.name,
          title: event.title,
          img: event.img,
          description: event.description,
          tag: event.tag,
          type: event.type,
          status: event.status
        }
      }).then(res => { return res }).catch(console.error)
    case "delete":
      return goodDB.doc(event.id).remove().then(res => { return res }).catch(console.error)
    case "update":
      return goodDB.doc(event.id).update({
        data: {
          i_id: event.i_id,
          price: event.price,
          number: event.number,
          name: event.name,
          title: event.title,
          img: event.img,
          description: event.description,
          tag: event.tag,
          type: event.type,
          status: event.status
        }
      }).then(res => { return res }).catch(console.error)
    case "select":
      switch (event.key) {
        case "_id":
          return goodDB.where({
            _id: event.value
          }).get().then(res => { return res }).catch(console.error)
        case "i_id":
          return goodDB.where({
            i_id: event.value
          }).orderBy("order", "asc").get().then(res => { return res }).catch(console.error)
      }
  }
}